
 /**  
 * Project Name:zhonghecrm  
 * File Name:SysUserServiceImpl.java  
 * Package Name:com.crm.service.impl  
 * Date:2016年10月12日下午6:09:05   
 *  
*/  
  
package com.crm.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.crm.dao.SysUserMapper;
import com.crm.model.SysMenu;
import com.crm.model.SysUser;
import com.crm.service.SysUserService;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.crm.util.UUIDUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**  
 * ClassName:SysUserServiceImpl <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月12日 下午6:09:05 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService{
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	
	@Override
	public int insert(SysUser record) {
		
		SysUser user = sysUserMapper.selectByAccount(record.getAccount());
		if(user != null){
			throw new DuplicateKeyException("用户名重复！");
		}
		
		SysUser user2 = sysUserMapper.selectByAccount(record.getMobile());
		if(user2 != null){
			throw new DuplicateKeyException("手机号重复！");
		}
		
		String uuid = UUIDUtil.getUUID();
		record.setUserId(uuid);
		record.setPassword(UUIDUtil.getMD5(record.getPassword(), uuid));
		
		return super.insert(record);
	}

	@Override
	public void changePassword(String userId, String newPassword) {
		SysUser user = new SysUser();
		user.setUserId(userId);
		user.setPassword(newPassword);
		sysUserMapper.changePassword(user); 
	}

	@Override
	public SysUser findByAccount(String account) {

		return sysUserMapper.selectByAccount(account);
	}

	@Override
	public List<SysMenu> findMenusByAccount() {
		  
		return null;
	}

	@Override
	public Set<Map<String, Object>> findResourceByAccount() {
		  
		return null;
	}

	@Override
	public List<SysUser> selectByUserId(Page<SysUser> page, SysUser Record) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		SysUser sy =  (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		String userId = sy.getUserId();
		Record.setUserId(userId);
		List<SysUser> list = sysUserMapper.selectByUserId(Record);
		return list;
	}

	@Override
	public List<SysUser> selectByDepartmentUserId(Page<SysUser> page, SysUser record) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "sort_code asc");
		List<SysUser> list = sysUserMapper.selectByDepartmentUserId(record);
		return list;
	}
	
	public List<SysUser> selectByDuty(SysUser t){
		return sysUserMapper.selectByDuty(t);
	}

	@Override
	public List<SysUser> selectUserByDutyId(Page<SysUser> page,SysUser record) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), "su.create_date desc");
		return sysUserMapper.selectUserByDutyId(record);
	}

	@Override
	public List<String> selectUsersByDutyIds(List<String> list) {
		return sysUserMapper.selectUsersByDutyIds(list);
	}

	@Override
	public int updateHeadImage(SysUser t) {
		return sysUserMapper.updateHeadImage(t);
	}

	@Override
	public int updateIsOnline(String userId, String isOnline) {
		
		return sysUserMapper.updateIsOnline(userId, isOnline);
	}
	
	@Override
	public void updatePasswordBatch(){
		
		List<SysUser> list = sysUserMapper.selectAll();
		
		for(SysUser user : list){
			user.setPassword(UUIDUtil.getMD5("123456", user.getUserId()));
			sysUserMapper.updateByPrimaryKey(user);
			System.out.println(user.getRealName() + ":更新成功！");
		}
		
	}

}
  
